package com.example.sixtrainingdemo.view;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.example.sixtrainingdemo.R;

public class RoundActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout mTabOne;
    private LinearLayout mTabAdd;
    private RoundFragment mRoundFragment;
    private Fragment mContentFragment;
    private FragmentManager mFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_round);
        mTabAdd = findViewById(R.id.ll_tab_add);
        mTabOne = findViewById(R.id.main_bottom);
        mTabAdd.setOnClickListener(this);
        initFragment();
        changeFragment(mRoundFragment);
        mFragmentManager.beginTransaction()
                .add(R.id.fragment_content, mRoundFragment).commitAllowingStateLoss();
        mContentFragment = mRoundFragment;
        mTabOne.setSelected(true);

    }

    private void initFragment() {
        mFragmentManager = getSupportFragmentManager();
        mRoundFragment = new RoundFragment();
    }

    public void changeFragment(Fragment to) {
        if (mContentFragment != null && mContentFragment != to) {
            if (!to.isAdded()) {
                mFragmentManager.beginTransaction().hide(mContentFragment)
                        .add(R.id.fragment_content, to).commitAllowingStateLoss();
            } else {
                mFragmentManager.beginTransaction().hide(mContentFragment)
                        .show(to).commitAllowingStateLoss();
            }
            mContentFragment = to;
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.main_bottom) {
            changeFragment(mRoundFragment);
        } else if (id == R.id.ll_tab_add) {
            Intent intent = new Intent(RoundActivity.this, AddRoundActivity.class);
            startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(
                    RoundActivity.this).toBundle());
        }
    }
}